﻿using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Input;
using System.Diagnostics;
using BBInterfaceNET.CommonServices;

namespace BBInterfaceNET.Toolbox.Behaviors
{
    public class DragStartBehavior:Behavior<FrameworkElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            // Hook up event handlers.
            this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            //detach the events
            this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
        }
        private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //get the object and initiate the dragdrop
            object dataContext = AssociatedObject.DataContext;
            object data = null;
            if (dataContext is ToolboxItem)
            {
                ToolboxItem item = dataContext as ToolboxItem;
                data = item.Type.AssemblyQualifiedName;
            }
            if (data == null) return;
            Debug.WriteLine("Drag started in toolbox");
            DragDrop.DoDragDrop(AssociatedObject, data, DragDropEffects.Copy);            
        }
    }
}
